 #include "menu.h"
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include <random>
#include <string>
#include <fstream>


void menu(sf::RenderWindow& window){
	sf::Texture menuTextures1, menuTextures12, menuTextures3, menuTextures33, menuBackground;
	menuBackground.loadFromFile("Image/menuBackground.png");
	menuTextures1.loadFromFile("Image/new_game.png");
	menuTextures12.loadFromFile("Image/new_game_star.png");
	menuTextures3.loadFromFile("Image/exit.png");
	menuTextures33.loadFromFile("Image/exit_star.png");
	sf::Sprite menuBg(menuBackground), menu1(menuTextures1), menu12(menuTextures12), menu3(menuTextures3), menu33(menuTextures33);
	bool isMenu = true;
	int menuNum = 0;
	menuBg.setPosition(0, 0);
	menu1.setPosition(800, 250);
	menu1.scale(sf::Vector2f(4, 4));
	menu12.setPosition(2000, 2000);
	menu12.scale(sf::Vector2f(4, 4));
	menu3.setPosition(800, 400);
	menu3.scale(sf::Vector2f(4, 4));
	menu33.setPosition(2000, 2000);
	menu33.scale(sf::Vector2f(4, 4));


	//Ìåíþ
	while (isMenu) {
		menu1.setColor(sf::Color::White); //Îêðàøèâàåì èêîíêè â áåëûé, ýòî íóæíî ÷òîáû îíè âîçðàùàëèñü â áåëûé öâåò ïîñëå íàâåäåíèÿ íà íèõ ìûøüþ
		menu12.setPosition(2000, 2000);
		menu33.setPosition(2000, 2000);
		menu3.setColor(sf::Color::White);
		menuNum = 0;

		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();
		}

		//çäåñü ÿ ïèøó õèòáîêñû äëÿ ìåíþøêè è â ñêîáêàõ ïèøó ÷òî îíà äîëæíà äåëàòü ïðè íàâåäåíèå íà ýòîò õèòáîêñ
		if (sf::IntRect(800, 400, 400, 100).contains(sf::Mouse::getPosition(window))) { menu12.setPosition(800, 250); menuNum = 1; }
		if (sf::IntRect(800, 550, 400, 100).contains(sf::Mouse::getPosition(window))) { menu33.setPosition(800, 400); menuNum = 3; }

		if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
		{
			if (menuNum == 1) isMenu = false;//åñëè íàæàëè ïåðâóþ êíîïêó, òî âûõîäèì èç ìåíþ
			/*if (menuNum == 2) { window.draw(about); window.display(); while (!Keyboard::isKeyPressed(Keyboard::Escape)); }*/
			if (menuNum == 3) { window.close(); isMenu = false; }//åñëè íàæàëè íà nhtnm. êíîïêó òî âûõîäèì

		}

		window.draw(menuBg);
		window.draw(menu1);
		window.draw(menu12);
		window.draw(menu3);
		window.draw(menu33);
		window.display();
	}
}

void menu2(sf::RenderWindow& window, int coin, int record) {
	sf::Texture menu0, menu1, menu2, menu3, menu4, menu5, menu6, menu7, menu8, menu9, menu10, menu11, menu12;
	menu0.loadFromFile("Image/menu0.png");
	menu1.loadFromFile("Image/menu1.png");
	menu2.loadFromFile("Image/menu2.png");
	menu3.loadFromFile("Image/menu3.png");
	menu4.loadFromFile("Image/menu4.png");
	menu5.loadFromFile("Image/menu5.png");
	menu6.loadFromFile("Image/menu6.png");
	menu7.loadFromFile("Image/menu7.png");
	menu8.loadFromFile("Image/menu8.png");
	menu9.loadFromFile("Image/menu9.png");
	menu10.loadFromFile("Image/menu10.png");
	menu11.loadFromFile("Image/menu11.png");
	menu12.loadFromFile("Image/menu12.png");

	sf::Sprite menu_0(menu0), menu_1(menu1), menu_2(menu2), menu_3(menu3), menu_4(menu4), menu_5(menu5), menu_6(menu6), menu_7(menu7), menu_8(menu8), menu_9(menu9), menu_10(menu10), menu_11(menu11), menu_12(menu12);

	sf::Clock clock;
	float totalTime = 0.0f;
	bool check_time = true;
	bool isMenu = true;
	bool new_record = false;
	while (window.isOpen() && check_time == true) {
		sf::Time elapsed = clock.restart();
		float deltaTime = elapsed.asSeconds(); // Ïîëó÷àåì ïðîøåäøåå âðåìÿ â ñåêóíäàõ
		totalTime += deltaTime; // Óâåëè÷èâàåì îáùåå âðåìÿ

		// ðàñïàêîâûâàåì øðèôò
		sf::Font font;
		font.loadFromFile("vcrosdmonorusbyd.ttf"); //ïîäðóáàåì øðèôò
		sf::Text text("", font, 80); // ñîçäà¸ì îáúåêò òåêñò
		text.setFillColor(sf::Color::Yellow);
		text.setStyle(sf::Text::Bold); //äåëàåì òåêñò æèðíûì 
		//ðåêîðä
		sf::Text text_record("", font, 80); // ñîçäà¸ì îáúåêò òåêñò
		text_record.setFillColor(sf::Color::Yellow);
		text_record.setStyle(sf::Text::Bold); //äåëàåì òåêñò æèðíûì

		//îáíîâëÿåì ðåêîðä
		if (record < coin) {
			std::ofstream outputFile("record.txt"); // Îòêðûâàåì ôàéë äëÿ çàïèñè
			outputFile << coin << std::endl; // Çàïèñü ðåêîðäà â ôàéë
			outputFile.close();
			new_record = true;
		}

		window.clear(); // Î÷èùàåì ýêðàí ïåðåä îòðèñîâêîé ñïðàéòîâ

		if (totalTime > 0.1) {
			window.draw(menu_0);
		}
		if (totalTime > 0.2) {
			window.draw(menu_1);
		}
		if (totalTime > 0.3) {
			window.draw(menu_2);
		}
		if (totalTime > 0.4) {
			window.draw(menu_3);
		}
		if (totalTime > 0.5) {
			window.draw(menu_4);
		}
		if (totalTime > 0.6) {
			window.draw(menu_5);
		}
		if (totalTime > 0.7) {
			window.draw(menu_6);
		}
		if (totalTime > 0.8) {
			window.draw(menu_7);
		}
		if (totalTime > 0.9) {
			window.draw(menu_8);
		}
		if (totalTime > 1) {
			window.draw(menu_9);
		}
		if (totalTime > 1.1) {
			window.draw(menu_10);
		}
		if (totalTime > 1.2) {
			window.draw(menu_11);
		}
		if (totalTime > 1.3) {
			std::string str_coin = std::to_string(coin); //ïåðåâîäèì ÷èñëî â ñòðîêó
			text.setString(str_coin);
			text.setPosition(970, 360); //ñ÷¸ò

			std::string str_record = std::to_string(record); //ïåðåâîäèì ÷èñëî â ñòðîêó
			text_record.setString(str_record);
			text_record.setPosition(970, 495); //ðåêîðä

			window.draw(menu_12);
			window.draw(text); //ñ÷¸ò
			window.draw(text_record); //ñ÷¸ò

			if (new_record) {
				sf::Text text_new_record("NEW RECORD", font, 80); // ñîçäà¸ì îáúåêò òåêñò
				text_new_record.setFillColor(sf::Color::Red);
				text_new_record.setStyle(sf::Text::Bold); //äåëàåì òåêñò æèðíûì
				text_new_record.setPosition(650, 800); //êîðäû
				window.draw(text_new_record);
				//написал комментарий
			}

			check_time = false;
		}
		window.display();
	}
	sf::sleep(sf::seconds(4.0f));
}